package cn.yunyichina.provider.base.test.order;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.yunyichina.provider.base.dao.OrderDao;
import cn.yunyichina.provider.base.entity.Order;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.RefundClinicInsuranceVo;
import cn.yunyichina.utils.convert.ObjectParser;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;

public class QueryOrder {

	static ClassPathXmlApplicationContext applicationContext=null;
	
	static{
		applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		applicationContext.start();	
	}
	
	@Test
	public void queryOder(){

    	OrderDao orderDao=(OrderDao)applicationContext.getBean("orderDao");
        Order wheres=new Order();
        wheres.setOrgCode("xgdxszyy");
        wheres.setIsInsurance((short)1);
        wheres.setBeginPayTime("2017-03-01 00:00:00");
        wheres.setEndPayTime("2017-03-15 23:59:59");
        
        List<Order> searchOrders=orderDao.select(wheres);
	 	
        FileOutputStream out;
		try {
			out = new FileOutputStream("D:/gangda_payment.csv");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
        PrintStream p=new PrintStream(out);
        p.println("ORDERNO,FPHM,MZLSH,ZFXM,JE,TRANTYPE");
      
        for(Order o: searchOrders){
        	Map maps=JSON.parseObject(o.getExtraParams(),Map.class);
        	String orderNum=o.getOrderNo();
        	if(!orderNum.startsWith("Y520")){
        		String hisOrderNum=o.getHisOrderNo();
            	String ssFeeNo=maps.containsKey("ss_fee_no")?maps.get("ss_fee_no").toString():"数据库没存";
            	
            	if(maps.containsKey("payment")){
    	        	String paymentstr=maps.get("payment").toString();
    	        	String[] payment=paymentstr.split("\\|");
    	        	for(int i=0;i<payment.length;i++){
    	        		String[] strs=payment[i].split(":");
    	        		p.println(orderNum+","+hisOrderNum+","+ssFeeNo+","+strs[0]+","+ObjectParser.toDouble(strs[1])/100+",");
    	        	}
            	}else{
            		p.println("请手工补齐，数据库没存"+orderNum+","+hisOrderNum+","+ssFeeNo+",,,");
            	}
        	}
        }
        
        try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        p.close();
        
        try {
			out = new FileOutputStream("D:/gangda_settlement.csv");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
        
        p=new PrintStream(out);
        p.println("ORDERNO,FPHM,MZLSH,JSXM,JE,TRANTYPE");
      
        for(Order o: searchOrders){
        	Map maps=JSON.parseObject(o.getExtraParams(),Map.class);
        	String hisOrderNum=o.getHisOrderNo();
        	String orderNum=o.getOrderNo();
        	if(!orderNum.startsWith("Y520")){
	        	String ssFeeNo=maps.containsKey("ss_fee_no")?maps.get("ss_fee_no").toString():"";
		        if(maps.containsKey("settlement")){
		        	String settlementstr=maps.get("settlement").toString();
		        	String[] settlement=settlementstr.split("\\|");
		        	for(int i=0;i<settlement.length;i++){
		        		String[] strs=settlement[i].split(":");
		        		p.println(orderNum+","+hisOrderNum+","+ssFeeNo+","+strs[0]+","+ObjectParser.toDouble(strs[1])/100+",");
		        	}
	        	}else{
	        		p.println("请手工补齐，数据库没存"+orderNum+","+hisOrderNum+","+ssFeeNo+",,,");
	        	}
        	}
        }
        
        try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        p.close();
		 
	       
	}
}
